package com.clkj.cldisabledmanclient.base;

import android.app.ProgressDialog;
import android.content.Intent;
import android.support.v4.app.Fragment;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.clkj.cldisabledmanclient.R;
import com.clkj.cldisabledmanclient.constant.ShareKey;
import com.clkj.cldisabledmanclient.mvp.login.ActivityLogin;
import com.clkj.cldisabledmanclient.util.CommonUtil;
import com.clkj.cldisabledmanclient.util.Res;
import com.clkj.cldisabledmanclient.util.ToastUtil;


/**
 * Created by Administrator on 2017/6/20.
 */

public abstract class BaseFragment extends Fragment {

    public abstract void initData();

    public abstract void assignView(View view);

    public abstract void initView();


    public boolean isLogin()
    {
        return (boolean) CommonUtil.getShareValue(ShareKey.LOGIN_TAG,false);
    }


    public String getUserId()
    {
        return (String) CommonUtil.getShareValue(ShareKey.USER_IDCARD,"");

    }


    public void toLogin(boolean isShowToast,String toastTip)
    {
        if (isShowToast)
        {

            ToastUtil.showShort(getActivity(), toastTip);

        }

        Intent toLoginIntent = new Intent(getActivity(), ActivityLogin.class);
        getActivity().startActivity(toLoginIntent);

    }



    ProgressDialog mProgressDialog;

    public void showProgressDialog(boolean canBack, String loadingMsg) {

        if (null!= getActivity() && getActivity().isDestroyed() == false)
        {
            if (mProgressDialog != null) {
                mProgressDialog.cancel();
                mProgressDialog = null;
            }

            //progressDialog.show会生成新的progressDialog
            if (TextUtils.isEmpty(loadingMsg) == false)
            {

                if (null == mProgressDialog)
                {
                    mProgressDialog = mProgressDialog.show(getActivity(),"提示",loadingMsg);
                }

            }else {


                if (null == mProgressDialog)
                {
                    mProgressDialog =  mProgressDialog.show(getActivity(),"提示","正在加载中，请稍后...");
                }

            }

            if (canBack) {
                mProgressDialog.setCanceledOnTouchOutside(true);
            } else {
                mProgressDialog.setCanceledOnTouchOutside(false);
            }
            mProgressDialog.setCancelable(true);





        }



    }



    public void dismissProgressDialog() {

        if(getActivity() != null && !getActivity().isDestroyed()) {

            /*if (null != progressDialog && progressDialog.isShowing()) {
                //progressDialog.cancel();
                progressDialog.dismiss();
            }*/

            if (null != mProgressDialog && mProgressDialog.isShowing()) {
                //progressDialog.cancel();

                mProgressDialog.dismiss();
            }

        }


    }


    private RelativeLayout layoutTitle;
    private ImageView ivBack;
    private TextView tvTitle;
    public TextView tvRight;

    public void initTitle(View view, Integer backgroundResId, Integer titleColorResId, String title, boolean isLeftBack, String rightTvStr) {
        layoutTitle = (RelativeLayout) view.findViewById(R.id.rl_title);
        ivBack = (ImageView) view.findViewById(R.id.iv_back);
        tvTitle = (TextView) view.findViewById(R.id.tv_title);
        tvRight = (TextView) view.findViewById(R.id.tv_right);


        if (null != backgroundResId) {
            layoutTitle.setBackgroundResource(backgroundResId);
        }

        if (null != titleColorResId) {
            tvTitle.setTextColor(Res.getColor(titleColorResId));
        }

        tvTitle.setText(title);

        if (isLeftBack == true) {
            ivBack.setVisibility(View.VISIBLE);
            ivBack.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    getActivity().finish();
                }
            });
        }

        if (null != rightTvStr) {
            tvRight.setText(rightTvStr);
            tvRight.setVisibility(View.VISIBLE);
        }

    }

    public void showToast(String tip)
    {
        if (null!= getActivity() && getActivity().isDestroyed() == false)
        {
            ToastUtil.showShort(getActivity(), tip);
        }
    }


}
